﻿<template>
  <div>
      <h3>当前求和:{{sum}}</h3>
      <h4>当前求和的值放大10倍:{{$store.getters.bigSum}}</h4>
      <h4>我在{{school}},学习{{subject}}</h4>
      <select name="" id="" v-model.number="n">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
      </select>
      <button @click="add">+</button>
      <button @click="jf">-</button>
      <button @click="jAdd">当前求和为奇数再加</button>
      <button @click="delayAdd">等一等再加</button>
  </div>
</template>

<script>
    import {mapState,mapGetters} from 'vuex'
    export default {
        name: 'Add',
        components:{},
        data() {
            return {
                n:1,
            }
        },
        methods: {
            add(){
                this.$store.dispatch('jia',this.n)
            },
            jf(){
                this.$store.dispatch('jian',this.n)
            },
            jAdd(){
                this.$store.dispatch('jadd',this.n)
            },
            delayAdd(){
                this.$store.dispatch('delayAdd',this.n)
            }
        },
        mounted(){ 
            console.log('Add',this.$store)
            // const x = mapState({sum:'sum',school:'school',subject:'subject'})
            // console.log(x)
        },
        computed:{
            //靠程序员亲自去写的计算属性:sum、school、subject
            /* sum(){
                return this.$store.state.sum
            },
            school(){
                return this.$store.state.school
            },
            subject(){
                return this.$store.state.subject
            },
            */

           //...:代表把对象里面的每一组k-v分开和放在对象里面
           //也就是把sum、school、subject都分开放到这里面了
           //借助mapState生成计算属性，从state中读取数据(对象写法)
        //    ...mapState({sum:'sum',school:'school',subject:'subject'}),
            ...mapState(['sum','school','subject']),

            ...mapGetters(['bigSum'])
            /* bigSum(){
                return this.$store.getters.bigSum
            } */
        },
    }
</script>

<style scoped>

</style>